# Archlive <http://archlive-pkg.googlecode.com>

pkgname=(catalyst catalyst-build-fglrx catalyst-utils)
pkgbase=catalyst
pkgver=11.5
_kernver=`uname -r`
pkgrel=${_kernver/-/_}
arch=('i686' 'x86_64')
url="http://www.ati.amd.com"
license=('custom')
makedepends=('gcc-libs' 'gcc>4.0.0' 'make' 'patch')
source=(
    http://www2.ati.com/drivers/linux/ati-driver-installer-${pkgver/./-}-x86.x86_64.run
    autofglrx.sh
    hook-fglrx
    catalyst_build_module
    makefile_compat.patch
    no_bkl.patch
    2.6.39_bkl.patch
    2.6.38_console.patch
    amdcccle.desktop
    atieventsd.sh
    catalyst.sh
    catalyst-PKGBUILD
    ati_make.sh
    )

build() {

	## Unpack archive
	msg "Unpacking archive ..."
	/bin/sh ./ati-driver-installer-${pkgver/./-}-x86.x86_64.run --extract archive_files

	if [ "${CARCH}" = "x86_64" ]; then
		BUILDARCH=x86_64
		_archdir=x86_64
	fi
	if [ "${CARCH}" = "i686" ]; then
		BUILDARCH=i386
		_archdir=x86
	fi

	cd "${srcdir}/archive_files/"
	patch -Np1 -i ../makefile_compat.patch || return 1
	if [[ ${_kernver:0:6} < "2.6.39" ]] && [[ `cat /lib/modules/${_kernver}/build/.config | grep -c CONFIG_BKL=y` = 0 ]]; then
		patch -Np1 -i ../no_bkl.patch || return 1
	fi
	if [[ ${_kernver:0:6} == "2.6.39" ]] || [[ ${_kernver:0:6} > "2.6.39" ]]; then
		patch -Np1 -i ../no_bkl.patch || return 1
		patch -Np1 -i ../2.6.39_bkl.patch || return 1
	fi

	. $startdir/ati_make.sh
	_ati_check

	cd "${srcdir}/archive_files/common/lib/modules/fglrx/build_mod"
	cp "${srcdir}/archive_files/arch/${_archdir}/lib/modules/fglrx/build_mod/libfglrx_ip.a" . || return 1
	cp 2.6.x/Makefile . || return 1

	## Compiling module using _ati_check variables
	make -C /lib/modules/${_kernver}/build SUBDIRS="`pwd`" ARCH=${BUILDARCH} \
	MODFLAGS="-DMODULE -DATI -DFGL -DPAGE_ATTR_FIX=$PAGE_ATTR_FIX -DCOMPAT_ALLOC_USER_SPACE=$COMPAT_ALLOC_USER_SPACE $def_smp $def_modversions" \
	PAGE_ATTR_FIX=$PAGE_ATTR_FIX COMPAT_ALLOC_USER_SPACE=$COMPAT_ALLOC_USER_SPACE modules || return 1
}

package_catalyst () {
	pkgdesc="AMD/ATI drivers."
	depends=("kernel26=${_kernver}")
	provides=("catalyst=${pkgver}")
	install=catalyst.install

	sed -i -e "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/" $startdir/*.install

	_archdir=x86_64
	test "${CARCH}" = "i686" && _archdir=x86

	cd "${srcdir}/archive_files/common/lib/modules/fglrx/build_mod"
	cp "${srcdir}/archive_files/arch/${_archdir}/lib/modules/fglrx/build_mod/libfglrx_ip.a" . || return 1
	cp 2.6.x/Makefile . || return 1

	install -m755 -d "${pkgdir}/lib/modules/${_kernver}/video/" || return 1
	install -m644 fglrx.ko "${pkgdir}/lib/modules/${_kernver}/video/" || return 1
	gzip -9 ${pkgdir}/lib/modules/${_kernver}/video/fglrx.ko
}

package_catalyst-build-fglrx () {
	pkgdesc="Sources and autobuild system of AMD/ATI drivers."
	provides=("catalyst-daemon=${pkgver}" "catalyst-hook=${pkgver}" "catalyst-generator=${pkgver}")
	install=build-fglrx.install

	if [ "${CARCH}" = "i686" ]; then
		cd "${srcdir}/archive_files/arch/x86/usr" || return 1
		_lib=lib
	elif [ "${CARCH}" = "x86_64" ]; then
		cd "${srcdir}/archive_files/arch/x86_64/usr" || return 1
		_lib=lib64
	fi

	# Prepare modules source files
	_archdir=x86_64
	test "${CARCH}" = "i686" && _archdir=x86
	cd "${srcdir}/archive_files/"
	install -m755 -d "${pkgdir}/usr/share/ati/build_mod/src" || return 1
	install -m644 common/lib/modules/fglrx/build_mod/*.c \
		"${pkgdir}/usr/share/ati/build_mod/src/" || return 1
	install -m644 common/lib/modules/fglrx/build_mod/*.h \
		"${pkgdir}/usr/share/ati/build_mod/src/" || return 1
	install -m644 common/lib/modules/fglrx/build_mod/2.6.x/Makefile \
		"${pkgdir}/usr/share/ati/build_mod/src/" || return 1
	install -m644 arch/${_archdir}/lib/modules/fglrx/build_mod/libfglrx_ip.a \
		"${pkgdir}/usr/share/ati/build_mod/src/" || return 1

	# patch for 2.6.38 kernel
	install -m644 "${srcdir}/2.6.38_console.patch" "${pkgdir}/usr/share/ati/build_mod/" || return 1

	install -m755 -d "${pkgdir}/usr/bin" || return 1
	install -m755 "${srcdir}/catalyst_build_module" "${pkgdir}/usr/bin" || return 1
	sed -i -e "s/catver=.*/catver=${pkgver}/" "${pkgdir}/usr/bin/catalyst_build_module"

	# patch for x86_64 kernel that was patched with very important patch that fix some critical security hole :> patch!
	install -m644 "${srcdir}/makefile_compat.patch" "${pkgdir}/usr/share/ati/build_mod/" || return 1

	# catalyst pkgbuild
	install -m644 "${srcdir}/catalyst-PKGBUILD" "${pkgdir}/usr/share/ati/build_mod/PKGBUILD" || return 1
	sed -i -e "s/pkgver=.*/pkgver=${pkgver}/" "${pkgdir}/usr/share/ati/build_mod/PKGBUILD" || return 1
	sed -i -e "s/pkgrel=.*/pkgrel=${pkgrel}/" "${pkgdir}/usr/share/ati/build_mod/PKGBUILD" || return 1

	# catalyst.install
	install -m644 "${startdir}/catalyst.install" "${pkgdir}/usr/share/ati/build_mod/catalyst.install" || return 1

	# modified ati's make.sh script
	install -m755 "${startdir}/ati_make.sh" "${pkgdir}/usr/share/ati/build_mod/" || return 1

	# optional patch
#	for ver in ${patch_ver}; do
#	install -m644 "${srcdir}/${patch_file}" \
#		"${pkgdir}/usr/share/ati/build_mod/${ver}.patch" || return 1
#	done

	# hook fglrx
	install -m755 -d "${pkgdir}/lib/initcpio/install" || return 1
	install -m644 "${srcdir}/hook-fglrx" "${pkgdir}/lib/initcpio/install/fglrx" || return 1

	# fglrx daemon
	install -m755 -d "${pkgdir}/etc/rc.d" || return 1
	install -m755 "${srcdir}/autofglrx.sh" "${pkgdir}/etc/rc.d/autofglrx" || return 1

	sed -i -e "s/catver=.*/catver=${pkgver}/" ${startdir}/*.install
}

package_catalyst-utils () {
	pkgdesc="AMD/ATI Catalyst drivers utilities and libraries."
	depends=('xorg-server>=1.9.0' 'xorg-server<1.10.0' 'netkit-bsd-finger' 'libxrandr' 'libsm' 'fontconfig' 'libxcursor' 'libxi' 'gcc-libs')
	optdepends=('qt: to run ATi Catalyst Control Center (amdcccle)')
	replaces=('libgl')
	conflicts=('catalyst-test' 'nvidia-utils' 'libgl')
	provides=('libgl' "libatical=${pkgver}")
	install=catalyst-utils.install

  ## Install userspace tools and libraries
    # Create directories
      install -m755 -d "${pkgdir}/etc/ati"
      install -m755 -d "${pkgdir}/etc/rc.d"
      install -m755 -d "${pkgdir}/etc/profile.d"
      install -m755 -d "${pkgdir}/etc/acpi/events"
      install -m755 -d "${pkgdir}/etc/security/console.apps"

      install -m755 -d "${pkgdir}/usr/lib/xorg/modules/dri"
      install -m755 -d "${pkgdir}/usr/lib/xorg/modules/drivers"
      install -m755 -d "${pkgdir}/usr/lib/xorg/modules/extensions"
      install -m755 -d "${pkgdir}/usr/lib/xorg/modules/extensions/fglrx" # since 11.4
      install -m755 -d "${pkgdir}/usr/lib/xorg/modules/linux"
      install -m755 -d "${pkgdir}/usr/lib/dri"
      install -m755 -d "${pkgdir}/usr/lib/fglrx" # since 11.4

      install -m755 -d "${pkgdir}/usr/bin"
      install -m755 -d "${pkgdir}/usr/sbin"

      install -m755 -d "${pkgdir}/usr/include/X11/extensions"
      install -m755 -d "${pkgdir}/usr/include/GL"

      install -m755 -d "${pkgdir}/usr/share/applications"
      install -m755 -d "${pkgdir}/usr/share/ati/amdcccle"
      install -m755 -d "${pkgdir}/usr/share/licenses/${pkgname}"
      install -m755 -d "${pkgdir}/usr/share/man/man8"
      install -m755 -d "${pkgdir}/usr/share/pixmaps"

    # X.org driver
      if [ "${CARCH}" = "i686" ]; then
	cd "${srcdir}/archive_files/xpic/usr/X11R6/lib/modules" || return 1
      elif [ "${CARCH}" = "x86_64" ]; then
	cd "${srcdir}/archive_files/xpic_64a/usr/X11R6/lib64/modules" || return 1
      fi

     # *.a added in 11.2, and removed in 11.3...
      #install -m644 *.a "${pkgdir}/usr/lib/xorg/modules/" || return 1
      install -m755 *.so "${pkgdir}/usr/lib/xorg/modules/" || return 1
      install -m755 drivers/*.so "${pkgdir}/usr/lib/xorg/modules/drivers/" || return 1
      install -m755 linux/*.so "${pkgdir}/usr/lib/xorg/modules/linux/" || return 1
      #install -m755 extensions/libglx.so "${pkgdir}/usr/lib/xorg/modules/extensions/" || return 1 #before 11.4
      install -m755 extensions/fglrx/fglrx-libglx.so "${pkgdir}/usr/lib/xorg/modules/extensions/fglrx/fglrx-libglx.so" || return 1 # since 11.5
      ln -snf /usr/lib/xorg/modules/extensions/fglrx/fglrx-libglx.so "${pkgdir}/usr/lib/xorg/modules/extensions/libglx.so" # since 11.4
      #install -m755 extensions/libdri.so "${pkgdir}/usr/lib/xorg/modules/extensions/libdri.ati" || return 1

    # Controlcenter / libraries
      if [ "${CARCH}" = "i686" ]; then
	cd "${srcdir}/archive_files/arch/x86/usr" || return 1
	_lib=lib
      elif [ "${CARCH}" = "x86_64" ]; then
	cd "${srcdir}/archive_files/arch/x86_64/usr" || return 1
	_lib=lib64
      fi

      install -m755 X11R6/bin/* "${pkgdir}/usr/bin/" || return 1
      install -m755 sbin/* "${pkgdir}/usr/sbin/" || return 1
      #install -m755 X11R6/${_lib}/*.so* "${pkgdir}/usr/lib/" || return #before 11.4
      install -m755 X11R6/${_lib}/fglrx/fglrx-libGL.so.1.2 "${pkgdir}/usr/lib/fglrx" || return 1 # since 11.5
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 "${pkgdir}/usr/lib/fglrx/libGL.so.1.2" # since 11.4
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 "${pkgdir}/usr/lib/fglrx-libGL.so.1.2" # since 11.4
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 "${pkgdir}/usr/lib/libGL.so.1.2" # since 11.4
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 "${pkgdir}/usr/lib/libGL.so.1" # since 11.4
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 "${pkgdir}/usr/lib/libGL.so" # since 11.4
      install -m755 X11R6/${_lib}/libAMDXvBA.so.1.0 "${pkgdir}/usr/lib/" || return 1 # since 11.4
      ln -snf libAMDXvBA.so.1.0 "${pkgdir}/usr/lib/libAMDXvBA.so.1" # since 11.4
      ln -snf libAMDXvBA.so.1.0 "${pkgdir}/usr/lib/libAMDXvBA.so" # since 11.4
      install -m755 X11R6/${_lib}/libatiadlxx.so "${pkgdir}/usr/lib/" || return 1 # since 11.4
      install -m755 X11R6/${_lib}/libfglrx_dm.so.1.0 "${pkgdir}/usr/lib/" || return 1 # since 11.4
      install -m755 X11R6/${_lib}/libXvBAW.so.1.0 "${pkgdir}/usr/lib/" || return 1 # since 11.4
      ln -snf libXvBAW.so.1.0 "${pkgdir}/usr/lib/libXvBAW.so.1" # since 11.4
      ln -snf libXvBAW.so.1.0 "${pkgdir}/usr/lib/libXvBAW.so" # since 11.4
      install -m644 X11R6/${_lib}/*.a "${pkgdir}/usr/lib/" || return 1 # really needed?
      install -m644 X11R6/${_lib}/*.cap "${pkgdir}/usr/lib/" || return 1
      install -m755 X11R6/${_lib}/modules/dri/*.so "${pkgdir}/usr/lib/xorg/modules/dri/" || return 1
      install -m755 ${_lib}/*.so* "${pkgdir}/usr/lib/" || return 1

    ## QT libs (only 2 files) - un-comment 2 lines below if you don't want to install qt package
#      install -m755 -d "${pkgdir}/usr/share/ati/${_lib}"
#      install -m755 share/ati/${_lib}/*.so* "${pkgdir}/usr/share/ati/${_lib}/" || return 1

      ln -snf /usr/lib/xorg/modules/dri/fglrx_dri.so ${pkgdir}/usr/lib/dri/fglrx_dri.so
      ln -snf libfglrx_dm.so.1.0 "${pkgdir}/usr/lib/libfglrx_dm.so.1"
      ln -snf libfglrx_dm.so.1.0 "${pkgdir}/usr/lib/libfglrx_dm.so"
      #ln -sf libfglrx_pp.so.1.0 "${pkgdir}/usr/lib/libfglrx_pp.so.1"
      #ln -sf libfglrx_tvout.so.1.0 "${pkgdir}/usr/lib/libfglrx_tvout.so.1"
      #ln -sf libfglrx_gamma.so.1.0 "${pkgdir}/usr/lib/libfglrx_gamma.so.1"
      #ln -sf libGL.so.1.2 "${pkgdir}/usr/lib/libGL.so.1" #before 11.4
      #ln -sf libGL.so.1.2 "${pkgdir}/usr/lib/libGL.so" #before 11.4
      ln -snf libatiuki.so.1.0 "${pkgdir}/usr/lib/libatiuki.so.1"
      ln -snf libatiuki.so.1.0 "${pkgdir}/usr/lib/libatiuki.so"


      cd "${srcdir}"/archive_files/common
      install -m644 etc/ati/* "${pkgdir}/etc/ati/" || return 1
      chmod 755 "${pkgdir}/etc/ati/authatieventsd.sh" || return 1

      #security provided with 10.9, is it working fine?
      install -m644 etc/security/console.apps/amdcccle-su "${pkgdir}/etc/security/console.apps/" || return 1

     # *.h removed in 11.3...
      #install -m644 usr/X11R6/include/X11/extensions/*.h "${pkgdir}/usr/include/X11/extensions/" || return 1
      install -m644 usr/X11R6/bin/amdupdaterandrconfig "${pkgdir}/usr/bin/" || return 1
      install -m644 usr/include/GL/*.h "${pkgdir}/usr/include/GL/" || return 1
      install -m755 usr/sbin/*.sh "${pkgdir}/usr/sbin/" || return 1
      install -m644 usr/share/ati/amdcccle/* "${pkgdir}/usr/share/ati/amdcccle/" || return 1
      install -m644 usr/share/icons/*.xpm "${pkgdir}/usr/share/pixmaps/" || return 1
      install -m644 usr/share/man/man8/*.8 "${pkgdir}/usr/share/man/man8/" || return 1
      install -m644 "${srcdir}/amdcccle.desktop" "${pkgdir}/usr/share/applications/" || return 1

    # ACPI example files
      install -m755 usr/share/doc/fglrx/examples/etc/acpi/*.sh "${pkgdir}/etc/acpi/" || return 1
      sed -i -e 's/usr\/X11R6/usr/g' "${pkgdir}/etc/acpi/ati-powermode.sh" || return 1
      install -m644 usr/share/doc/fglrx/examples/etc/acpi/events/* "${pkgdir}/etc/acpi/events/" || return 1

    # Add ATI Events Daemon launcher
      install -m755 "${srcdir}/atieventsd.sh" "${pkgdir}/etc/rc.d/atieventsd" || return 1

    # thanks to cerebral, we dont need that damned symlink
      install -m755 "${srcdir}/catalyst.sh" "${pkgdir}/etc/profile.d/" || return 1

    # License
      install -m644 "${srcdir}/archive_files/ATI_LICENSE.TXT" "${pkgdir}/usr/share/licenses/${pkgname}/" || return 1
}

